<?php
/**
 * Created by PhpStorm.
 * User: yanghe
 * Date: 2016/5/10
 * Time: 16:52
 */
namespace MyClass\Admin;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Request;
class ImageBase
{
    /**
     * @param $img_data上传图片是保存数据库
     */
    static public function putImageByUE($img_data)
    {
        preg_match_all( '|<img[^>]+src="([^"]+)"|iUs', $img_data['content'], $list);
        foreach($list[1] as $v){
            //把文件相关数据插入数据库
            $input_data["image_article"] = $img_data['id'];//文章id
            $input_data['image_name'] = $img_data['title'];
            $input_data["image_path"] = $v;  //绝对路径
            $input_data['image_create_time']=date("y-m-d h:i:s",time());
            DB::table("image")->insertGetId($input_data);
        }
    }

    /**
     * @param $img_data修改文章是重新存入数据库
     */
    static public function uImage($img_data){
        DB::table('image')->where("image_article","=",$img_data['id'])->delete();
        preg_match_all( '|<img[^>]+src="([^"]+)"|iUs', $img_data['content'], $list);
        foreach($list[1] as $v){
            //把文件相关数据插入数据库
            $input_data["image_article"] = $img_data['id'];//文章id
            $input_data['image_name'] = $img_data['title'];
            $input_data["image_path"] = $v;  //绝对路径
            $input_data['image_update_time']=date("y-m-d h:i:s",time());
            $input_data['image_create_time']=date("y-m-d h:i:s",time());
            DB::table("image")->insert($input_data);
        }
    }

    /**
     * @return bool在图片管理中查看图片
     */
    static public function sImage(){
        $sort=Request::input("sort","image_id");
        $data["sort"] = $sort;

        if($sort !=null)
        {
            $data["inputData"]=DB::table("image")
                ->orderBy($sort,"desc")
                ->paginate(5);
            $data["fen"]=$data["inputData"]->render();
        }
        $data["nowPage"]=$data["inputData"]->currentPage();
        $data["allPage"]=$data["inputData"]->lastPage();
        if($data)
        {
            return $data;
        }
        else
        {
            return false;
        }
    }

    /**
     * @param $image_id
     * @return bool在图片管理中单独删除图片
     */
    static public function dImage($image_id){
        $data=DB::table('image')->where("image_id","=",$image_id)->get();
        $path=$data[0]->image_path;
        $file_path=$_SERVER['DOCUMENT_ROOT'].$path;
        if(DB::table("image")->where("image_id","=",$image_id)->delete()){
            $result = @unlink ($file_path);
            if($result){
                return true;
            }
            else{
                return false;
            }
        }else{
            return false;
        }
    }

    /**
     * @param $image_intro
     * @return bool删除文章时删除该文章所有图片
     */
    static public function dAllImage($data){
        foreach($data as $v){
            $file_path=$_SERVER['DOCUMENT_ROOT'].$v->image_path;
            @unlink ($file_path);
        }
        return true;

    }
}